package com.xmb.wechat.view.wechat.chat;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.GroupTalkBean;
import com.xmb.wechat.bean.WechatContactBean;
import com.xmb.wechat.lsn.SendMsgCenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class WechatChatAddBCardsActivity extends BaseActivity {

    @BindView(R2.id.tv_sender)
    TextView     mTvSender;
    @BindView(R2.id.iv_avatar)
    ImageView    mIvAvatar;
    @BindView(R2.id.rv)
    RecyclerView mRv;
    @BindView(R2.id.iv_b_cards)
    ImageView    mIvBCards;
    @BindView(R2.id.tv_b_cards)
    TextView     mTvBCards;

    private WechatContactBean mInitWechatContact, mSender, mBusinessCards;
    private GroupTalkBean                mInitGroupTalk;
    private ContactAdapter               adapter;
    private ArrayList<WechatContactBean> list;
    private boolean                      isRoomChat;

    public WechatChatAddBCardsActivity() {
        super(R.layout.wechat_activity_chat_add_b_cards);
    }

    public static void start(Context act, WechatContactBean contactBean) {
        Intent it = new Intent(act, WechatChatAddBCardsActivity.class);
        it.putExtra("isRoomChat", false);
        it.putExtra("WechatContactBean", contactBean);
        act.startActivity(it);
    }

    public static void start(Context act, GroupTalkBean groupTalkBean) {
        Intent it = new Intent(act, WechatChatAddBCardsActivity.class);
        it.putExtra("isRoomChat", true);
        it.putExtra("GroupTalkBean", groupTalkBean);
        act.startActivity(it);
    }

    @Override
    protected void initView() {
        list = new ArrayList<>();
        //添加数据（me）
        WechatContactBean me = WechatContactBean.instanceNewContactMe(this);
        onSenderChange(me);
        list.add(me);
        //添加数据（other）
        isRoomChat = getIntent().getBooleanExtra("isRoomChat", false);
        if (!isRoomChat) {//单聊
            mInitWechatContact = (WechatContactBean) getIntent().getSerializableExtra("WechatContactBean");
            list.add(mInitWechatContact);
        } else {//群聊
            mInitGroupTalk = (GroupTalkBean) getIntent().getSerializableExtra("GroupTalkBean");
            list.addAll(mInitGroupTalk.getGroupMembersList());
        }
        //发送者选择adapter
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mRv.setLayoutManager(layoutManager);
        adapter = new ContactAdapter(R.layout.wechat_select_sender_contact_item, list);
        mRv.setAdapter(adapter);
    }

    @OnClick({R2.id.ll_sender, R2.id.ll_b_cards, R2.id.btn_save})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.ll_sender) {
            if (mRv.getVisibility() == View.VISIBLE) {
                mRv.setVisibility(View.GONE);
            } else {
                mRv.setVisibility(View.VISIBLE);
            }
        } else if (id == R.id.ll_b_cards) {
            showRoleChooseSender(this,false,false,null);
        } else if (id == R.id.btn_save) {
            if (mBusinessCards == null) {
                ToastUtils.showShort("请选择要发送的名片");
                return;
            }
            SendMsgCenter.sendBuinessCards(new SendMsgCenter.SendMsgListener() {
                @Override
                public void onSendSuc() {
                    finish();
                }

                @Override
                public void onSendError(String errorMsg) {

                }
            }, mSender.getId(), isRoomChat, isRoomChat ? mInitGroupTalk.getId() : mInitWechatContact.getId(), mBusinessCards);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE_OTHER_SEND || requestCode == REQUEST_CODE_CHOOSE_NEW_ADD_SEND) {
            if (data == null)
                return;
            WechatContactBean result = (WechatContactBean) data.getSerializableExtra("result");
            if (result == null)
                return;
            //赋值
            mBusinessCards = result;
            //显示界面
            WechatContactBean.setupAvatarIntoImageView(mBusinessCards, mIvBCards, 12);
            mTvBCards.setText(TextUtils.isEmpty(mBusinessCards.getName()) ? "" : mBusinessCards.getName());
            //修改按钮状态
        }
    }

    public class ContactAdapter extends BaseQuickAdapter<WechatContactBean, BaseViewHolder> {
        public ContactAdapter(int layoutResId, List data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final WechatContactBean vo) {
            helper.setText(R.id.name, vo.getName());
            WechatContactBean.setupAvatarIntoImageView(vo, helper.getView(R.id.iv_avatar), 12);
            helper.setChecked(R.id.rb, vo.isSelected());
            helper.getView(R.id.vg).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (WechatContactBean item : ContactAdapter.this.getData()) {
                        item.setSelected(false);
                    }
                    notifyDataSetChanged();
                    onSenderChange(vo);
                }
            });
        }
    }

    //选中后改变布局显示
    private void onSenderChange(WechatContactBean vo) {
        vo.setSelected(true);
        mSender = vo;
        mRv.setVisibility(View.GONE);
        WechatContactBean.setupAvatarIntoImageView(vo, mIvAvatar, 12);
        mTvSender.setText(vo.getName());
    }
}
